const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_time: 0,
    goods_category_list: {},
    goods_list: [],
    category_id: 0,
    page: 1,
    timer_array: {},
    //轮播图属性
    adv_img: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
    indicator_color: '#AAA',
    indicator_active_color: '#FFF',
    swiper_height: 150,
    aClickFlag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let category_id = options.category_id;

    if (category_id != undefined){
      that.setData({
        category_id: category_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let base = that.data.Base;

    app.restStatus(that, 'aClickFlag');
    app.sendRequest({
      url: 'System.Shop.advDetail',
      data: {
        'ap_keyword': 'APPLET_DISCOUNT_SWIPER',
        'export_type' : 'data'
      },
      success: function (res) {
        if (res.code == 0) {
          let data = res.data;
          data.advs = app.checkEmpty(data.advs, []);
          for (let index in data.advs) {
            data.advs[index].adv_image = app.IMG(data.advs[index].adv_image);
            if (index == 0) {
              app.getWindowSize(that, true, data.advs[index].adv_image);
            }
          }

          that.setData({
            adv_img: data.advs,
          })
        }
      }
    });
    app.defaultImg(that, true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.clearTimer(this)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.clearTimer(this)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    let goods_list = that.data.goods_list;
    let page = that.data.page;
    let category_id = that.data.category_id
    let timer_array = that.data.timer_array;

    app.sendRequest({
      url: 'System.Goods.goodsDiscountList',
      data: {
        category_id: category_id,
        page_index: page
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          let new_goods_list = data.data;
          let param = {};
          let param_key = ''
          if (new_goods_list.length > 0){
            page++;
          }
          
          let new_timer_array = {};
          for (let index in new_goods_list) {
            //图片处理
            let img = new_goods_list[index].pic_cover_small;
            new_goods_list[index].pic_cover_small = app.IMG(img);

            let key = goods_list.length;
            new_timer_array[key] = {};
            new_timer_array[key].end_time = new_goods_list[index].end_time;
            
            that.timing(that, new_timer_array, key);
            param_key = 'goods_list[' + key + ']';
            param[param_key] = new_goods_list[index];
            that.setData(param);
          }

          that.setData({
            page: page
          })
        }
      }
    });
  },

  /**
   * 图片加载获取高度
   */
  imgLoad: function(e) {
    let res = wx.getSystemInfoSync();
    let height = e.detail.height;
    let width = e.detail.width;
    let rate = width / height;
    let swiper_height = res.windowWidth / rate;
    
    this.setData({
      swiperHeight: swiper_height
    })
  },

  /**
   * 获取折扣商品
   */
  loadInfo: function (){
    let that = this;
    let category_id = that.data.category_id;

    app.sendRequest({
      url: 'System.Goods.goodsDiscountList',
      data: {
        category_id: category_id,
        page_index: 1
      },
      success: function (res) {
        console.log(res);
        let code = res.code;
        if (code == 0) {
          let goods_list = res.data.data;

          let timer_array = {};
          for (let index in goods_list){
            let img = goods_list[index].pic_cover_small;
            goods_list[index].pic_cover_small = app.IMG(img);

            timer_array[index] = {};
            timer_array[index].end_time = goods_list[index].end_time;
          }
          for (let key in timer_array){
            that.timing(that, timer_array, key);
          }
          that.setData({
            goods_list: goods_list,
            page: 2
          })
        }
      }
    });
  },

  /**
   * 限时折扣图片加载失败
   */
  errorDiscountImg: function (e) {
    
  },

  /**
   * 计时
   */
  timing: function (that, timer_array, index){
    let current_time = that.data.current_time;
    let count_second = (timer_array[index].end_time - current_time);

    if (count_second > 0) {
      count_second--;
      let day = Math.floor((count_second / 3600) / 24);
      let hour = Math.floor((count_second / 3600) % 24);
      let minute = Math.floor((count_second / 60) % 60);
      let second = Math.floor(count_second % 60);
      let param = {};
      let param_key = 'timer_array[' + index + '].nowtime';
      param[param_key] = day + '天' + hour + '小时' + minute + '分' + second + '秒';

      that.setData(param);
    } else {
      timer_array[index].nowtime = '活动已结束 !';

      that.setData({
        timer_array: timer_array
      })
    }
    let timer = setInterval(function(){
      if (count_second > 0){
        count_second--;
        let day = Math.floor((count_second / 3600) / 24);
        let hour = Math.floor((count_second / 3600) % 24);
        let minute = Math.floor((count_second / 60) % 60);
        let second = Math.floor(count_second % 60);
        let param = {};
        let param_key = 'timer_array[' + index + '].nowtime';
        param[param_key] = day + '天' + hour + '小时' + minute + '分' + second + '秒';

        that.setData(param);
      }else{
        let param = {};
        let param_key = 'timer_array[' + index + '].nowtime';
        param[param_key] = '活动已结束！';
        param_key = 'timer_array[' + index + '].timer';
        param[param_key] = null;

        that.setData(param);
        clearInterval(timer);
      }
    },1000)
    let param = {};
    let param_key = 'timer_array[' + index + '].timer';
    param[param_key] = timer;
    that.setData(param);
  },

  /**
   * 链接跳转
   */
  linkJump: function (e) {
    let url = e.currentTarget.dataset.url;
    app.linkJumpDetection(url);
  }
})